package spider

import (
	"asocrawler/conf"
	"asocrawler/db"
	"asocrawler/tool"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"runtime/debug"
	"strconv"
	"strings"
	"time"
)

const (
	xinDouApi = "https://gw.newrank.cn/api/xd/xdnphb/nr/cloud/douyin/new/rank/hotAwemeRecord"
	xinShiApi = "https://gw.newrank.cn/api/xs/nr/xinshi/like/getLike"

	xinKuaiLoginApi = "https://xk.newrank.cn/xdnphb/nr/cloud/ks/user/login"
	xinKuaiApi      = "https://xk.newrank.cn/xdnphb/nr/cloud/ks/material/hotPhotoCourier"

	xinZhanLoginApi = "https://xz.newrank.cn/nr/bili/login/saveAndLogin"
	xinZhanApi      = "https://xz.newrank.cn/nr/bili/search/getHotVideo"
)

type NewRankSpider struct {
	ChannelName  string
	TimeInterval int
	Platform     string
}

func New_NewRankSpider() (v *NewRankSpider) {
	v = &NewRankSpider{
		ChannelName:  "新榜",
		Platform:     "视频",
		TimeInterval: 2,
	}
	return
}

func (this NewRankSpider) Run() {
	go func() {
		this.run()
	}()
}
func (this *NewRankSpider) Println(v ...interface{}) {
	var str = fmt.Sprintf("[%s]-[%s]", this.ChannelName, this.Platform)
	if conf.Conf.Debug.Switch {
		str += "-[Debug]"
	}
	log.Println(str, fmt.Sprintln(v...))
}

type LoginResult struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func (this *NewRankSpider) LoginForStatus() {
	client := &http.Client{}

	/* 快手 */
	tryTimes1 := 0
re:
	req, err := http.NewRequest("POST", xinKuaiLoginApi, nil)
	if err != nil {
		return
	}

	req.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)

	resp, err := client.Do(req)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var p LoginResult
	err = json.Unmarshal(b, &p)
	if err != nil {
		this.Println("xinKuai login error,err=", err, string(b))
		return
	}
	if p.Code != 2000 {
		if tryTimes1 < 10 {
			this.Println("xinKuai login p.Code != 2000,尝试重新请求...tryTimes：", tryTimes1)
			tryTimes1++
			time.Sleep(500 * time.Millisecond)
			goto re
		}
		this.Println("xinKuai login 登录失败...,data=", string(b))
		return
	}

	/*  B站  */
	tryTimes2 := 0
	params := `{"source":"22","fromUrl":"https://xd.newrank.cn/home/index","unit":null,"keyword":null}`
re2:

	req2, err := http.NewRequest("POST", xinZhanLoginApi, strings.NewReader(params))
	if err != nil {
		return
	}

	req2.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)

	resp2, err := client.Do(req2)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp2.Body.Close()
	b2, err := ioutil.ReadAll(resp2.Body)
	if err != nil {
		return
	}

	var p2 LoginResult
	err = json.Unmarshal(b2, &p2)
	if err != nil {
		this.Println("xinZhan login error,err=", err, string(b2))
		return
	}
	if p2.Code != 2000 {
		if tryTimes2 < 10 {
			this.Println("xinZhan login p.Code != 2000,尝试重新请求...tryTimes：", tryTimes2)
			tryTimes2++
			time.Sleep(500 * time.Millisecond)
			goto re2
		}
		this.Println("xinZhan login 登录失败...,date=", string(b))
		return
	}
}

// 新抖
type XinDouResult struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data struct {
		List []struct {
			AnaAddCacl []struct {
				AddForwardCount string `json:"add_forward_count"` // 新增转发
				AddShareCount   string `json:"add_share_count"`   // 新增分享
				AddCommentCount string `json:"add_comment_count"` // 新增评论
				AddDiggCount    string `json:"add_digg_count"`    // 新增获赞

				UseForwardCount string `json:"use_forward_count"` // 总转发
				UseShareCount   string `json:"use_share_count"`   // 总分享
				UseCommentCount string `json:"use_comment_count"` // 总评论
				UseDiggCount    string `json:"use_digg_count"`    // 总获赞
			} `json:"ana_add_cacl"`
			AwemeDesc    string `json:"aweme_desc"`     // 视频标题
			AwemeId      string `json:"aweme_id"`       // 视频id
			CoverUrl     string `json:"cover_url"`      // 视频封面
			CreateTime   string `json:"create_time"`    // 视频发布日期
			ShareUrl     string `json:"share_url"`      // 视频源地址
			AuthorUserId string `json:"author_user_id"` // 发布人id
			UserAvatar   string `json:"user_avatar"`    // 发布人头像
			UserNickName string `json:"user_nickname"`  // 发布人昵称
			UserType     string `json:"user_type"`      // 发布人类型
		} `json:"list"`
	} `json:"data"`
}

func (this *NewRankSpider) CollectXinDouData(date string) (rs []db.NewRankVideoResult, err error) {
	tryTimes := 0
	client := &http.Client{}
	params := fmt.Sprintf(`{"sort":"add_digg_count","userType":"","recordTime":"%s","start":1,"size":100}`, date)

re:
	req, err := http.NewRequest("POST", xinDouApi, strings.NewReader(params))
	if err != nil {
		return
	}

	req.Header.Set("n-token", conf.Conf.NewRankConfig.NewRankToken)
	req.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)
	req.Header.Set("content-type", "application/json;charset=UTF-8")

	resp, err := client.Do(req)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var p XinDouResult
	err = json.Unmarshal(b, &p)
	if err != nil {
		this.Println("CollectXinDouData error,err=", err, string(b))
		return
	}
	if p.Code != 2000 {
		if tryTimes < 10 {
			this.Println("CollectXinDouData p.Code != 2000,尝试重新请求...tryTimes：", tryTimes)
			tryTimes++
			time.Sleep(500 * time.Millisecond)
			goto re
		}
		err = errors.New("p.Code != 2000,p=" + string(b))
		return
	}

	if len(p.Data.List) > 0 {
		for _, ps := range p.Data.List {

			// 新增数据转int
			AddForwardCount, err := strconv.Atoi(ps.AnaAddCacl[0].AddForwardCount)
			if err != nil {
				this.Println("CollectXinDouData AddForwardCount Atoi error")
				return rs, err
			}
			AddShareCount, err := strconv.Atoi(ps.AnaAddCacl[0].AddShareCount)
			if err != nil {
				this.Println("CollectXinDouData AddShareCount Atoi error")
				return rs, err
			}
			AddCommentCount, err := strconv.Atoi(ps.AnaAddCacl[0].AddCommentCount)
			if err != nil {
				this.Println("CollectXinDouData AddCommentCount Atoi error")
				return rs, err
			}
			AddDiggCount, err := strconv.Atoi(ps.AnaAddCacl[0].AddDiggCount)
			if err != nil {
				this.Println("CollectXinDouData AddDiggCount Atoi error")
				return rs, err
			}

			// 总数据转int
			UseForwardCount, err := strconv.Atoi(ps.AnaAddCacl[0].UseForwardCount)
			if err != nil {
				this.Println("CollectXinDouData UseForwardCount Atoi error")
				return rs, err
			}
			UseShareCount, err := strconv.Atoi(ps.AnaAddCacl[0].UseShareCount)
			if err != nil {
				this.Println("CollectXinDouData UseShareCount Atoi error")
				return rs, err
			}
			UseCommentCount, err := strconv.Atoi(ps.AnaAddCacl[0].UseCommentCount)
			if err != nil {
				this.Println("CollectXinDouData UseCommentCount Atoi error")
				return rs, err
			}
			UseDiggCount, err := strconv.Atoi(ps.AnaAddCacl[0].UseDiggCount)
			if err != nil {
				this.Println("CollectXinDouData UseDiggCount Atoi error")
				return rs, err
			}

			// "2022-01-18 14:19:25+08"
			CreateTime := strings.Replace(ps.CreateTime, "+08", "", -1)

			vPlatform := "抖音"
			VideoId := ps.AwemeId + "-" + vPlatform
			UserId := ps.AuthorUserId + "-" + vPlatform
			UserAvatar := strings.Replace(ps.UserAvatar, ".heic", ".jpg", -1)

			r := db.NewRankVideoResult{
				VideoId:       VideoId,
				VideoTitle:    ps.AwemeDesc,
				VideoPlatform: vPlatform,
				CoverUrl:      ps.CoverUrl,
				PublishTime:   CreateTime,
				OriginUrl:     ps.ShareUrl,

				UserId:       UserId,
				UserAvatar:   UserAvatar,
				UserNickName: ps.UserNickName,
				UserType:     ps.UserType,

				AddForwardCount: AddForwardCount,
				AddShareCount:   AddShareCount,
				AddCommentCount: AddCommentCount,
				AddDiggCount:    AddDiggCount,

				TotalForwardCount: UseForwardCount,
				TotalShareCount:   UseShareCount,
				TotalCommentCount: UseCommentCount,
				TotalDiggCount:    UseDiggCount,

				Date: date,
			}

			rs = append(rs, r)
		}
	}

	return
}

// 新视
type XinShiResult struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data struct {
		List []struct {
			CommentCount string `json:"commentCount"` // 总评论量
			LikeCount    string `json:"likeCount"`    // 总点赞量

			VideoId     string `json:"videoId"`     // 视频id
			CoverUrl    string `json:"coverUrl"`    // 视频封面
			Description string `json:"description"` // 视频标题
			PublishTime string `json:"publishTime"` // 发布时间

			Uid      string `json:"uid"`      // 发布人id
			HeadUrl  string `json:"headUrl"`  // 发布人头像
			NickName string `json:"nickname"` // 发布人昵称
			Type     string `json:"type"`     // 发布人类型

		} `json:"list"`
	} `json:"data"`
}

func (this *NewRankSpider) CollectXinShiData(date string) (rs []db.NewRankVideoResult, err error) {
	tryTimes := 0
	client := &http.Client{}
	// created 1 "昨天" 3 "三天前"
	params := `{"created":"30","sort":"like_count","type":"","size":20,"start":1}`
re:
	req, err := http.NewRequest("POST", xinShiApi, strings.NewReader(params))
	if err != nil {
		return
	}

	req.Header.Set("n-token", conf.Conf.NewRankConfig.NewRankToken)
	req.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)
	req.Header.Set("content-type", "application/json;charset=UTF-8")

	resp, err := client.Do(req)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var p XinShiResult
	err = json.Unmarshal(b, &p)
	if err != nil {
		this.Println("CollectXinShiData error,err=", err, string(b))
		return
	}
	if p.Code != 2000 {
		if tryTimes < 10 {
			this.Println("CollectXinShiData p.Code != 2000,尝试重新请求...tryTimes：", tryTimes)
			tryTimes++
			time.Sleep(500 * time.Millisecond)
			goto re
		}
		err = errors.New("p.Code != 2000,p=" + string(b))
		return
	}

	if len(p.Data.List) > 0 {
		for _, ps := range p.Data.List {

			TotalCommentCountStr := strings.Replace(ps.CommentCount, "万+", "0000", -1)
			TotalDiggCountStr := strings.Replace(ps.LikeCount, "万+", "0000", -1)

			TotalCommentCount, err := strconv.Atoi(TotalCommentCountStr)
			if err != nil {
				this.Println("CollectXinShiData CommentCount Atoi error")
				return rs, err
			}
			TotalDiggCount, err := strconv.Atoi(TotalDiggCountStr)
			if err != nil {
				this.Println("CollectXinShiData LikeCount Atoi error")
				return rs, err
			}

			vPlatform := "微视"
			VideoId := ps.VideoId + "-" + vPlatform
			UserId := ps.Uid + "-" + vPlatform

			r := db.NewRankVideoResult{
				VideoId:       VideoId,
				VideoTitle:    ps.Description,
				VideoPlatform: vPlatform,
				CoverUrl:      ps.CoverUrl,
				PublishTime:   ps.PublishTime,

				UserId:       UserId,
				UserAvatar:   ps.HeadUrl,
				UserNickName: ps.NickName,
				UserType:     ps.Type,

				TotalCommentCount: TotalCommentCount,
				TotalDiggCount:    TotalDiggCount,

				Date: date,
			}

			rs = append(rs, r)
		}
	}

	return
}

// 新快
type XinKuaiResult struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type XinKuaiData struct {
	List []struct {
		PhotoId            string `json:"photoId"` // 视频id
		Caption            string `json:"caption"` // 视频标题
		Time               string `json:"time"`    // 视频发布日期
		CoverThumbnailUrls []struct {
			Cdn string `json:"cdn"`
			Url string `json:"url"` // 视频封面
		} `json:"coverThumbnailUrls"`
		ShareInfo string `json:"shareInfo"` // 原视频信息 "userId=3xrw45hr5g7vd3w&photoId=3xp2s9mjr7hw7rs"

		UserId      string `json:"userId"`      // 发布人id
		UserName    string `json:"userName"`    // 发布人昵称
		UserType    string `json:"userType"`    // 发布人类型
		UserHeadUrl string `json:"userHeadurl"` // 发布人头像

		AnaAdd struct {
			AddCommentCount int `json:"addCommentCount"` // 新增评论量
			AddShareCount   int `json:"addShareCount"`   // 新增分享量
			AddLikeCount    int `json:"addLikeCount"`    // 新增点赞量
			AddViewCount    int `json:"addViewCount"`    // 新增播放量
			AddForwardCount int `json:"addForwardCount"` // 新增转发量 ??

			UseCommentCount int `json:"useCommentCount"` // 总评论量
			UseShareCount   int `json:"useShareCount"`   // 总分享量
			UseLikeCount    int `json:"useLikeCount"`    // 总点赞量
			UseViewCount    int `json:"useViewCount"`    // 总播放量
			UseForwardCount int `json:"useForwardCount"` // 总转发量 ??

		} `json:"anaAdd"`
	} `json:"list"`
}

func (this *NewRankSpider) CollectXinKuaiData(date string) (rs []db.NewRankVideoResult, err error) {
	tryTimes := 0
	client := &http.Client{}

	// 无论参数如何，默认返200条
	timeLayout := "2006-01-02"
	pDate, err := time.ParseInLocation(timeLayout, date, time.Local)
	if err != nil {
		return
	}
	oldTime := pDate.AddDate(0, 0, -1).Format(timeLayout)
	params := fmt.Sprintf(`{"sort":"addViewCount","userType":"","recordTime":"%s","start":1,"size":300}`, oldTime)
re:
	req, err := http.NewRequest("POST", xinKuaiApi, strings.NewReader(params))
	if err != nil {
		return
	}

	req.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)

	resp, err := client.Do(req)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var p XinKuaiResult
	err = json.Unmarshal(b, &p)
	if err != nil {
		this.Println("CollectXinKuaiData error,err=", err, string(b))
		return
	}
	if p.Code != 2000 {
		if tryTimes < 10 {
			this.Println("CollectXinKuaiData p.Code != 2000,尝试重新请求...tryTimes：", tryTimes)
			tryTimes++
			time.Sleep(500 * time.Millisecond)
			goto re
		}
		err = errors.New("p.Code != 2000,p=" + string(b))
		return
	}

	msg, flag := p.Data.(string)
	if flag && msg == "默认数据为空" {
		log.Println("快手默认数据为空...")
		return
	}

	var data XinKuaiData
	pByte, err := json.Marshal(p.Data)
	if err != nil {
		return
	}
	err = json.Unmarshal(pByte, &data)

	if len(data.List) > 0 {
		for _, ps := range data.List {
			var VideoId, UserId, CoverUrl, OriginUrl string
			if len(ps.CoverThumbnailUrls) > 0 {
				CoverUrl = ps.CoverThumbnailUrls[0].Url
			}

			shareInfo := strings.Split(ps.ShareInfo, "&")
			for _, s := range shareInfo {
				if strings.Contains(s, "photoId=") {
					photoId := strings.Replace(s, "photoId=", "", -1)
					OriginUrl = fmt.Sprintf("https://www.kuaishou.com/short-video/%s", photoId)
					break
				}
			}

			vPlatform := "快手"
			VideoId = ps.PhotoId + "-" + vPlatform
			UserId = ps.UserId + "-" + vPlatform

			r := db.NewRankVideoResult{
				VideoId:       VideoId,
				VideoTitle:    ps.Caption,
				VideoPlatform: vPlatform,
				CoverUrl:      CoverUrl,
				PublishTime:   ps.Time,
				OriginUrl:     OriginUrl,

				UserId:       UserId,
				UserAvatar:   ps.UserHeadUrl,
				UserNickName: ps.UserName,
				UserType:     ps.UserType,

				AddForwardCount: ps.AnaAdd.AddForwardCount,
				AddShareCount:   ps.AnaAdd.AddShareCount,
				AddCommentCount: ps.AnaAdd.AddCommentCount,
				AddDiggCount:    ps.AnaAdd.AddLikeCount,
				AddViewCount:    ps.AnaAdd.AddViewCount,

				TotalForwardCount: ps.AnaAdd.UseForwardCount,
				TotalShareCount:   ps.AnaAdd.UseShareCount,
				TotalCommentCount: ps.AnaAdd.UseCommentCount,
				TotalDiggCount:    ps.AnaAdd.UseLikeCount,
				TotalViewCount:    ps.AnaAdd.UseViewCount,

				Date: date,
			}

			rs = append(rs, r)
		}
	}

	return
}

// 新站
type XinZhanResult struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data struct {
		List []struct {
			AddShare   string `json:"addShare"`   // 新增分享
			AddLike    string `json:"addLike"`    // 新增点赞
			AddComment string `json:"addComment"` // 新增评论
			AddPlay    string `json:"addPlay"`    // 新增播放

			Like    string `json:"like"`    // 总点赞量
			Share   string `json:"share"`   // 总分享量
			Comment string `json:"comment"` // 总评论量
			Play    string `json:"play"`    // 总播放量

			PicUrl  string `json:"picUrl"`  // 视频封面
			Title   string `json:"title"`   // 视频标题
			Created string `json:"created"` // 视频发布时间
			Bvid    string `json:"bvid"`    // 视频id

			Up []struct {
				Face string `json:"face"` // 发布人头像
				Mid  string `json:"mid"`  // 发布人id
				Name string `json:"name"` // 发布人昵称
			} `json:"up"` // 发布人信息 数组只取第一个
		} `json:"list"`
	} `json:"data"`
}

func (this *NewRankSpider) CollectXinZhanData(date string) (rs []db.NewRankVideoResult, err error) {
	tryTimes := 0
	client := &http.Client{}

	params := fmt.Sprintf(`{"size":50,"start":1,"sort":"addPlay","ctype":"","secondType":"","date":"%s","dTags":""}`, date)

re:
	req, err := http.NewRequest("POST", xinZhanApi, strings.NewReader(params))
	if err != nil {
		return
	}

	req.Header.Set("cookie", conf.Conf.NewRankConfig.NewRankCookie)

	resp, err := client.Do(req)
	if err != nil {
		time.Sleep(1 * time.Second)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var p XinZhanResult
	err = json.Unmarshal(b, &p)
	if err != nil {
		this.Println("CollectXinZhanData error,err=", err, string(b))
		return
	}
	if p.Code != 2000 {
		if tryTimes < 10 {
			this.Println("CollectXinZhanData p.Code != 2000,尝试重新请求...tryTimes：", tryTimes)
			tryTimes++
			time.Sleep(500 * time.Millisecond)
			goto re
		}
		err = errors.New("p.Code != 2000,p=" + string(b))
		return
	}
	if len(p.Data.List) > 0 {
		for _, ps := range p.Data.List {
			var VideoId, UserId, UserNickName, UserAvatar, OriginUrl string

			vPlatform := "B站"
			OriginUrl = fmt.Sprintf("https://www.bilibili.com/video/%s", ps.Bvid)

			VideoId = ps.Bvid + "-" + vPlatform

			if len(ps.Up) > 0 {
				UserId = ps.Up[0].Mid + "-" + vPlatform
				UserNickName = ps.Up[0].Name
				UserAvatar = ps.Up[0].Face
			}

			// 新增量
			AddShare, err := this.ParseStrNumber(ps.AddShare)
			if err != nil {
				this.Println("CollectXinZhanData AddShare Atoi error")
				return rs, err
			}

			AddComment, err := this.ParseStrNumber(ps.AddComment)
			if err != nil {
				this.Println("CollectXinZhanData AddComment Atoi error")
				return rs, err
			}

			AddLike, err := this.ParseStrNumber(ps.AddLike)
			if err != nil {
				this.Println("CollectXinZhanData AddLike Atoi error")
				return rs, err
			}

			AddPlay, err := this.ParseStrNumber(ps.AddPlay)
			if err != nil {
				this.Println("CollectXinZhanData AddShare Atoi error")
				return rs, err
			}

			// 总量
			Share, err := this.ParseStrNumber(ps.Share)
			if err != nil {
				this.Println("CollectXinZhanData Share Atoi error")
				return rs, err
			}
			Comment, err := this.ParseStrNumber(ps.Comment)
			if err != nil {
				this.Println("CollectXinZhanData Comment Atoi error")
				return rs, err
			}
			Like, err := this.ParseStrNumber(ps.Like)
			if err != nil {
				this.Println("CollectXinZhanData Like Atoi error")
				return rs, err
			}
			Play, err := this.ParseStrNumber(ps.Play)
			if err != nil {
				this.Println("CollectXinZhanData Play Atoi error")
				return rs, err
			}

			r := db.NewRankVideoResult{
				VideoId:       VideoId,
				VideoTitle:    ps.Title,
				VideoPlatform: vPlatform,
				CoverUrl:      ps.PicUrl,
				PublishTime:   ps.Created,
				OriginUrl:     OriginUrl,

				UserId:       UserId,
				UserAvatar:   UserAvatar,
				UserNickName: UserNickName,

				AddShareCount:   AddShare,
				AddCommentCount: AddComment,
				AddDiggCount:    AddLike,
				AddViewCount:    AddPlay,

				TotalShareCount:   Share,
				TotalCommentCount: Comment,
				TotalDiggCount:    Like,
				TotalViewCount:    Play,

				Date: date,
			}

			rs = append(rs, r)
		}
	}

	return
}

func (this *NewRankSpider) ParseStrNumber(str string) (num int, err error) {
	if strings.Contains(str, "w") {
		//  1.62w的情况
		tempStr := strings.Replace(str, "w", "", -1)
		temp, err := strconv.ParseFloat(tempStr, 64)
		if err != nil {
			return num, err
		}
		num = int(temp * 10000)
	} else {
		num, err = strconv.Atoi(str)
		if err != nil {
			return num, err
		}
	}
	return
}
func (this *NewRankSpider) Collect(date string) (bool, error) {
	var err error
	// 刷新登录态，快手和B站平台需要
	//this.LoginForStatus()

	//新抖
	//rs1, err := this.CollectXinDouData(date)
	//if err != nil {
	//	return false, err
	//}
	//if len(rs1) == 0 {
	//	this.Println("CollectXinDouData len = 0")
	//	tool.SendWarn("ASO报警-newRank（新榜）爬虫报错", "ASO报警-newRank（新榜）抖音爬虫报错,错误信息：无数据，token可能过期")
	//} else {
	//	err := db.SaveNewRankData(rs1)
	//	if err != nil {
	//		return false, err
	//	}
	//}

	// 新视
	rs2, err := this.CollectXinShiData(date)
	if err != nil {
		return false, err
	}
	if len(rs2) == 0 {
		this.Println("CollectXinShiData len = 0")
		tool.SendWarn("ASO报警-newRank（新榜）爬虫报错", "ASO报警-newRank（新榜）微视爬虫报错,错误信息：无数据，token可能过期")
	} else {
		err := db.SaveNewRankData(rs2)
		if err != nil {
			return false, err
		}
	}

	// 新快
	/*rs3, err := this.CollectXinKuaiData(date)
	if err != nil {
		return false, err
	}
	if len(rs3) == 0 {
		this.Println("CollectXinKuaiData len = 0")
		tool.SendWarn("ASO报警-newRank（新榜）爬虫报错", "ASO报警-newRank（新榜）快手爬虫报错,错误信息：无数据，token可能过期")
	} else {
		err := db.SaveNewRankData(rs3)
		if err != nil {
			return false, err
		}
	}*/

	// 新站
	/*rs4, err := this.CollectXinZhanData(date)
	if err != nil {
		return false, err
	}
	if len(rs4) == 0 {
		this.Println("CollectXinZhanData len = 0")
		tool.SendWarn("ASO报警-newRank（新榜）爬虫报错", "ASO报警-newRank（新榜）B站爬虫报错,错误信息：无数据，token可能过期")
	} else {
		err := db.SaveNewRankData(rs4)
		if err != nil {
			return false, err
		}
	}*/

	return true, nil

}

func (this *NewRankSpider) run() {
	defer func() {
		if err := recover(); err != nil {
			errMsg := string(debug.Stack())
			this.Println("[崩溃]err=", err, errMsg)
		}
	}()
	if conf.Conf.Debug.Switch {
		for _, fDate := range conf.Conf.Debug.DateList {
			this.Println("日期:", fDate, "开始采集数据...")
			succ, err := this.Collect(fDate)
			if succ {
				this.Println("日期:", fDate, "采集数据完成")
			} else {
				if err == nil {
					this.Println("日期:", fDate, "数据未更新")
				} else {
					this.Println("日期:", fDate, "出现错误err:", err)
				}
			}
		}
	} else {
	re:
		fDate := time.Now().Format("2006-01-02")
		this.GetDataByDate(fDate)
		time.Sleep(time.Duration(this.TimeInterval) * time.Hour)
		goto re
	}
}

func (this *NewRankSpider) GetDataByDate(date string) {
	this.Println("日期:", date, "开始采集数据...")
	success, err := this.Collect(date)
	if err != nil {
		tool.SendWarn("ASO报警-newRank（新榜）爬虫报错", "ASO报警-newRank（新榜）爬虫报错,错误信息："+err.Error())
		this.Println("日期:", date, "采集失败 err=", err)
	} else {
		if success {
			this.Println("日期:", date, "采集数据完成 稍后再次采集...")
		} else {
			this.Println("日期:", date, "数据未更新 稍后重新采集...")
		}
	}
}
